﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MPlusTree;
using System.IO;

namespace TestMPlusTree
{
    class Program
    {
        private static void InputTree(MPTree<Key> tree)
        {
	        System.Console.WriteLine("\tINPUT TREE");
            
	        int num;
            string line;
            bool isExit = false;

            do
            {
	            System.Console.Write(">> Int number:\t");
                line = System.Console.ReadLine();

                if (int.TryParse(line, out num))
                {
                    Key key = new Key();
                    key.SetKeyValue(num);

                    MPEntry<Key> entry = new MPEntry<Key>();
                    entry.SetKey(key);
                    tree.Insert(entry);
                }
                else
                {
                    isExit = (line.ToLower().Trim() == "e" || line.ToLower().Trim() == "exit");
                    if (!isExit)
                    {
                        System.Console.WriteLine();
                        System.Console.WriteLine("You must input an integer number!");
                        System.Console.WriteLine();
                    }
                }
            }
            while (!isExit);
        }

        private static void Menu()
        {
            System.Console.WriteLine("+----------------------------------------------+");
            System.Console.WriteLine("|          M+ TREE - TESTING PROGRAM           |");
            System.Console.WriteLine("+----------------------------------------------+");

            System.Console.WriteLine("1. Print menu");
            System.Console.WriteLine("2. Input tree.");
            System.Console.WriteLine("3. Range searching.");
            System.Console.WriteLine("4. Nearest neighbour searching.");
            System.Console.WriteLine("5. Exit.");
        }

        private static void RangeSearch(MPTree<Key> tree)
        {
            System.Console.WriteLine("\tRANGE SEARCHING");

            int number;
            string line;
            do
            {
                System.Console.Write(">> Key number:\t");
                line = System.Console.ReadLine();
            }
            while (!int.TryParse(line, out number));

            MPQueryObj qObj = new MPQueryObj();
            Key key = new Key();
            key.SetKeyValue(number);
            qObj.SetKey(key);

            float radius;
            do
            {
                System.Console.Write(">> Radius:\t");
                line = System.Console.ReadLine();
            }
            while(!float.TryParse(line, out radius));

            LinkedList<MPKey> result = new LinkedList<MPKey>();
            tree.RangeSearch(qObj, radius, result);

            //print result
            System.Console.WriteLine();
            System.Console.WriteLine("-----------------------------");
            foreach(MPKey k in result)
                System.Console.WriteLine(" " + k.GetDimensionValue(0));
            System.Console.WriteLine("-----------------------------");
        }

        static void Main(string[] args)
        {
            MPTree<Key> tree = new MPTree<Key>();
            tree.CreateTree("MPlusTree_TESTING.mp", 2);
            Menu();
            int n;
            do
            {
                System.Console.WriteLine();
                System.Console.Write("# ");

                while(!int.TryParse(System.Console.ReadLine(), out n));

                switch (n)
                {
                    case 1:
                        System.Console.WriteLine();
                        System.Console.WriteLine();
                        Menu();
                        break;
                    case 2:
                        InputTree(tree);
                        break;
                    case 3:
                        RangeSearch(tree);
                        break;
                    case 4:

                        break;
                    case 5:

                        if (File.Exists("MPlusTree_TESTING.mp"))
                        {
                            tree.Close();
                            File.Delete("MPlusTree_TESTING.mp");
                        }

                        break;
                }
            }
            while (n != 5);
        }
    }
}
